package com.leetcode;

import java.util.Scanner;

public class DivideTwoIntegers {

	public static void main(String[] args) {

	///	System.out.println(Integer.MAX_VALUE);
	///	System.out.println(Integer.MIN_VALUE);

//		System.out.println(divide(-2147483648, -1));
//		System.out.println(divide(-2147483648,1));
		System.out.println(divide(-2147483648, -2));
		System.out.println(divide(-2147483648,2));
		System.out.println(divide(-2147483648, -5));
		System.out.println(divide(-2147483648,5));
	}

	public static int divide(int dividend, int divisor) {
		if (dividend == Integer.MIN_VALUE) {
			if (divisor == -1)
				return Integer.MAX_VALUE;
			if (divisor == 1)
				return dividend;
			if (divisor < -1||divisor>1) {
				int count = 0;
				while (dividend <= divisor) {
					count++;
					dividend = divisor>0?dividend+divisor:
							dividend - divisor;
				}
				if(divisor<0) return count;
				if(divisor>0) return 2-count;
			}

		}
		boolean flag = true;
		if (dividend <= 0 && divisor < 0) {
			dividend = Math.abs(dividend);
			divisor = Math.abs(divisor);

		} else if (dividend <= 0 && divisor > 0 || dividend >= 0 && divisor < 0) {
			dividend = Math.abs(dividend);
			divisor = Math.abs(divisor);
			flag = false;
		}
		if (dividend >= 0 && dividend < divisor)
			return 0;
		else if (dividend <=Integer.MAX_VALUE) {
			if(dividend==Integer.MAX_VALUE&&divisor==1) return dividend;
			int count = 0;
			while (dividend >= divisor) {
				count++;
				dividend = dividend - divisor;
			}
			if (flag == true)
				return count;
			else
				return 0 - count;
		} else {
			return Integer.MAX_VALUE;
		}
	}
}
